package com.aizuda.boot.modules.business.feishu.factory;

import com.aizuda.boot.modules.auth.domain.entity.ThirdUser;
import com.aizuda.boot.modules.business.feishu.domain.AtUsersDTO;
import com.aizuda.boot.modules.business.feishu.domain.BotValues;
import com.aizuda.boot.modules.business.feishu.strategy.TemplateParameterStrategy;
import com.aizuda.boot.modules.business.feishu.strategy.impl.AbstractTemplateParameterStrategy;
import com.aizuda.boot.modules.system.entity.SysMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

/**
 * 模版参数构建策略工厂类
 *
 * @author system
 * @date 2025-01-22
 */
@Slf4j
@Component
public class TemplateParameterStrategyFactory {


    private List<TemplateParameterStrategy> strategies;

    @Autowired
    public TemplateParameterStrategyFactory(List<TemplateParameterStrategy> strategies) {
        this.strategies = strategies;
        log.info("初始化模版参数构建策略工厂，共加载{}个策略", strategies.size());
    }

    /**
     * 根据租户Key和业务类型获取对应的策略
     *
     * @param tenantKey    租户标识
     * @param businessType 业务类型
     * @return 对应的策略实现
     */
    public TemplateParameterStrategy getStrategy(String tenantKey, String businessType) {
        log.debug("选择策略，tenantKey: {}, businessType: {}", tenantKey, businessType);

        // 遍历所有策略，找到第一个支持的策略
        for (TemplateParameterStrategy strategy : strategies) {
            if (strategy.supports(tenantKey, businessType)) {
                return strategy;
            }
        }
        return new AbstractTemplateParameterStrategy() {
            @Override
            public boolean supports(String tenantKey, String businessType) {
                return true;
            }

            @Override
            public BotValues buildTemplateParameters(SysMessage message, ThirdUser thirdUser) {
                BotValues botValues = new BotValues();
                botValues.setOpenId(thirdUser.getOpenId());
                botValues.setTenantKey(thirdUser.getTenantKey());
                botValues.setBusinessType(message.getBusinessType());
                HashMap<String, String> map = new HashMap<>();
                map.put("url", "http://fimai.aidb.com.cn:3013/business/contract/all-contracts");

                // 添加基础参数
                botValues.addParam("title", message.getTitle())
                        .addParam("content", message.getContent())
                        .addParam("alarm_time", getCurrentTime())
                        .addParam("type", getMessageTypeDescription(message.getBusinessType()))
                        .addParam("href", map);

                return botValues;
            }

            @Override
            public BotValues buildAtTemplate(AtUsersDTO dto, ThirdUser thirdUser) {
                BotValues botValues = new BotValues();
                HashMap<String, String> map = new HashMap<>();
                map.put("url", dto.getUrl());
                botValues.setOpenId(thirdUser.getOpenId());
                botValues.setTenantKey(thirdUser.getTenantKey());
                botValues.setBusinessType(dto.getBusinessType());
                botValues.addParam("title", dto.getTitle())
                        .addParam("content", dto.getContent())
                        .addParam("alarm_time", getCurrentTime())
                        .addParam("type", getMessageTypeDescription(dto.getBusinessType()))
                        .addParam("href", map);
                return botValues;
            }
        };
    }

    /**
     * 获取所有可用的策略
     *
     * @return 所有策略列表
     */
    public List<TemplateParameterStrategy> getAllStrategies() {
        return strategies;
    }
} 